package com.example.yls.demoa;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yls on 2018/6/14.
 */

public class CustomView extends View {
    private static int MAX_COUNT=10;
    private float w;
    private Paint paint;
    private List<Point> whith=new ArrayList<>();
    private List<Point> black=new ArrayList<>();
    private boolean isWhith=true;
    private Bitmap b;
    private Bitmap wh;

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint=new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        setBackgroundColor(0x00000000);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.w=w;
        Bitmap b1=BitmapFactory.decodeResource(getResources(),R.drawable.stone_w2);
        b=Bitmap.createScaledBitmap(b1,(int)((w/MAX_COUNT)-(w/MAX_COUNT)/2),(int)((w/MAX_COUNT)-(w/MAX_COUNT)/2),false);
        wh=Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.stone_w2),(int)((w/MAX_COUNT)-(w/MAX_COUNT)/2),(int)((w/MAX_COUNT)-(w/MAX_COUNT)/2),false);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawTable(canvas);
        drawQizi(canvas);
    }

    private void drawQizi(Canvas canvas) {
        Log.e("长度",whith.size()+"");
        Log.e("长度",black.size()+"");
        for (int i=0;i<black.size();i++){
            canvas.drawBitmap(b,(w/MAX_COUNT)/2+(w/MAX_COUNT)*black.get(i).x,(w/MAX_COUNT)/2+(w/MAX_COUNT)*black.get(i).y,paint);
        }
        for (int i=0;i<whith.size();i++){
            canvas.drawBitmap(wh,(w/MAX_COUNT)/2+(w/MAX_COUNT)*whith.get(i).x,(w/MAX_COUNT)/2+(w/MAX_COUNT)*whith.get(i).y,paint);
        }
    }

    public void drawTable(Canvas canvas){
        for (int i=0;i<MAX_COUNT;i++){
            float startX=(w/MAX_COUNT)/2+(w/MAX_COUNT)*i;
            float startY=(w/MAX_COUNT)/2+(w/MAX_COUNT)*i;
            float endX=w-(w/MAX_COUNT)/2;
            float endY=w-(w/MAX_COUNT)/2;
            canvas.drawLine((w/MAX_COUNT)/2,startY,endX,startY,paint);
            canvas.drawLine(startX,(w/MAX_COUNT)/2,startX,endY,paint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_UP:
                if (isWhith){
                    whith.add(checkWhith(event.getX(),event.getY()));
                    Log.e("x,y",event.getX()+","+event.getY());
                }else {
                    black.add(checkWhith(event.getX(),event.getY()));
                    Log.e("x,y",event.getX()+","+event.getY());
                }
                invalidate();
                isWhith=!isWhith;
                break;
        }
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width=MeasureSpec.getSize(widthMeasureSpec);
        int height=MeasureSpec.getSize(heightMeasureSpec);
        height=width;
        setMeasuredDimension(width,width);
    }

    public Point checkWhith(float x,float y){
        int mx=(int)((w/10)/x);
        int my=(int)((w/10)/y);
       // Log.e("x,y",mx+","+my);
        return new Point(mx,my);
    }
}
